﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MMO_MANAGER.DTO;
using MMO_MANAGER.BUS;
using MMO_MANAGER.Utils;

namespace MMO_MANAGER.FORM_CATALOG
{
    public partial class frmEmailList : DevExpress.XtraEditors.XtraForm
    {
        public frmEmailList()
        {
            InitializeComponent();
        }

        email_list_bus bus;
        email_list elbo;
        string chuoi = "mahoa";
        string NotEdit = "Not Edit Data!";
        private void frmEmailList_Load(object sender, EventArgs e)
        {
            bus = new email_list_bus();
            elbo = new email_list();
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = bus.GetData();
        }

        private void btn_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
            FillData();
        }

        private void btn_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
            FillData();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
            FillData();
        }

        private void btn_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FillData();
        }

        private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region Add Edit Delete

        void Add()
        {
            try
            {
                using (var frm = new EmailDlg())
                {
                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        elbo.ten = frm.NameEmail;
                        elbo.pwd = EasyEncoding.Encrypt(frm.PWD, chuoi);
                        elbo.ghichu = frm.Note;
                        elbo.active = 1;    //  active
                        elbo.ngayedit = DateTime.Now;

                        DataTable dt = bus.GetListEMailByName(elbo);
                        if (dt.Rows.Count == 0)
                        {
                            bus.Insert(elbo);
                            EasyDialog.ShowSuccessfulDialog("Thêm thành công!");
                            //lbl_totalrows.Text = gridView1.RowCount.ToString();
                        }
                        else
                        {
                            EasyDialog.ShowWarningDialog("Đã có email này rồi!");
                            return;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Edit()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                elbo.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                elbo.ten = gridView1.GetRowCellValue(row, "ten").ToString();
                //elbo.pwd = gridView1.GetRowCellValue(row, "pwd").ToString();
                elbo.ghichu = gridView1.GetRowCellValue(row, "ghichu").ToString();
                //elbo.active = gridView1.GetRowCellValue(row, "active").ToString();
                //elbo.ngayedit = gridView1.GetRowCellValue(row, "ngayedit").ToString();

                using (var frm = new EmailDlg())
                {
                    frm.txt_pass.ReadOnly = true;
                    frm.NameEmail = elbo.ten;
                    frm.PWD = NotEdit;
                    //frm.PWD = EasyEncoding.Descrypt(elbo.pwd, chuoi);
                    frm.Note = elbo.ghichu;

                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        elbo.ten = frm.NameEmail;
                        //elbo.pwd = EasyEncoding.Encrypt(frm.PWD, chuoi);
                        elbo.ghichu = frm.Note;
                        //elbo.active = 1;    //  active
                        elbo.ngayedit = DateTime.Now;
                        bus.Update(elbo);
                        EasyDialog.ShowSuccessfulDialog("Sửa thành công!");
                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Delete()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                elbo.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                DialogResult dr = EasyDialog.ShowYesNoDialog("Bạn có muốn xóa không?");
                if (dr == DialogResult.Yes)
                {
                    elbo.active = 0;
                    bus.Delete(elbo);
                    EasyDialog.ShowSuccessfulDialog("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        #endregion

        private void btn_copy_email_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            elbo.ten = gridView1.GetRowCellValue(row, "ten").ToString();
            Clipboard.SetText(elbo.ten);
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }

        private void btn_copy_pwd_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            elbo.ten = gridView1.GetRowCellValue(row, "pwd").ToString();
            Clipboard.SetText(EasyEncoding.Descrypt(elbo.ten, chuoi));
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }
    
    }
}